package com.ccp.dev.workflow.service;

import com.ccp.dev.core.basic.base.BaseService;
import com.ccp.dev.core.basic.util.ContextUtil;
import com.ccp.dev.core.basic.util.UUIDUtils;
import com.ccp.dev.core.util.BeanUtils;
import com.ccp.dev.system.model.GlobalType;
import com.ccp.dev.system.service.GlobalTypeService;
import com.ccp.dev.workflow.dao.TaskApprovalItemsDao;
import com.ccp.dev.workflow.model.TaskApprovalItems;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * ClassDescribe  常用语设置 Service类
 * @author: fanruiqi
 * Date: 2019-09-09
 * Since:1
 * To change this template use File | Settings | File Templates.
 */
@Service
public class TaskApprovalItemsService extends BaseService<TaskApprovalItems> {
    @Resource
    private TaskApprovalItemsDao taskApprovalItemsDao;
    @Resource
    private GlobalTypeService globalTypeService;


    /**
     * 根据itemId获取常用语
     * @param itemId itemId
     * @return 常用语
     */
    public TaskApprovalItems getById(String itemId) {
        return taskApprovalItemsDao.getById(itemId);
    }

    /**
     *根据expression和itemId获取常用语
     * @param expression expression
     * @param itemId     itemId
     * @return 常用语
     */
    public List getByExpression(String expression, String itemId) {
        return taskApprovalItemsDao.getByExpression(expression,itemId);
    }

    /**
     * 新增常用语
     * @param approvalItem
     * @param type
     * @param typeIdArr
     * @param modelKeyArr
     * @param currentUserId
     */
    public String addTaskApproval(String approvalItem, Short type, String typeIdArr, String modelKeyArr, String currentUserId) throws Exception {
        String[] expressions = approvalItem.split(",");
        if(type.shortValue()== TaskApprovalItems.TYPE_GLOBAL.shortValue()||type.shortValue()== TaskApprovalItems.TYPE_USER.shortValue()){
            //分类type=1或4，指全局常用语或只对于个人的常用语
            for (String expression:expressions){
                TaskApprovalItems taskApprovalItem = new TaskApprovalItems();
                taskApprovalItem.setItemId(UUIDUtils.getUUIDFor32());
                taskApprovalItem.setType(type);
                taskApprovalItem.setExpression(expression);
                taskApprovalItem.setUserId(currentUserId);
                taskApprovalItemsDao.add(taskApprovalItem);
            }
        }else if(type.shortValue()== TaskApprovalItems.TYPE_FLOWTYPE.shortValue()){
            //分类type=2,指对于流程分类的常用语
            if(BeanUtils.isEmpty(typeIdArr)){
                return "taskApproval.save.type.failure";
            }
            String[] typeIds = typeIdArr.split(",");
            for(String typeId:typeIds){
                for(String expression:expressions){
                    TaskApprovalItems taskApprovalItem = new TaskApprovalItems();
                    taskApprovalItem.setItemId(UUIDUtils.getUUIDFor32());
                    taskApprovalItem.setTypeId(typeId);
                    taskApprovalItem.setType(type);
                    taskApprovalItem.setExpression(expression);
                    taskApprovalItem.setUserId(currentUserId);
                    taskApprovalItemsDao.add(taskApprovalItem);
                }
            }
        }else if(type.shortValue()== TaskApprovalItems.TYPE_FLOW.shortValue()){
            //分类type=3，指对于流程的常用语
            if(BeanUtils.isEmpty(modelKeyArr)){
                return "taskApproval.save.act.failure";
            }
            String[] modelKeys = modelKeyArr.split(",");
            for(String modelKey:modelKeys){
                for(String expression:expressions){
                    TaskApprovalItems taskApprovalItem = new TaskApprovalItems();
                    taskApprovalItem.setItemId(UUIDUtils.getUUIDFor32());
                    taskApprovalItem.setModelKey(modelKey);
                    taskApprovalItem.setType(type);
                    taskApprovalItem.setExpression(expression);
                    taskApprovalItem.setUserId(currentUserId);
                    taskApprovalItemsDao.add(taskApprovalItem);
                }
            }
        }
        return null;
    }


    /**
     * 获取流程常用语
     * @param modelKey 流程定义key
     * @param typeId 流程定义所属的分类id
     * @return
     */
    public List<String> getApprovalByDefKeyAndTypeId(String modelKey,String typeId){
        List<String> taskAppItemsList = new ArrayList<String>();
        String curUserId=ContextUtil.getCurrentUserId();
        //获取全局常用语、对于流程的常用语、只对于个人的常用语（分类type=1、3、4）
        List<TaskApprovalItems> taskAppItem1=taskApprovalItemsDao.queryByModelKeyAndUserAndSys(modelKey,curUserId);
        if (BeanUtils.isNotEmpty(taskAppItem1)) {
            for(TaskApprovalItems taskAppItem:taskAppItem1){
                taskAppItemsList.add(taskAppItem.getExpression());
            }
        }
        if(BeanUtils.isEmpty(typeId)){
            return taskAppItemsList;
        }
        //获取对于流程分类的常用语（分类type=2）
        List<TaskApprovalItems> taskAppItem2=taskApprovalItemsDao.queryByType(TaskApprovalItems.TYPE_FLOWTYPE);
        if (BeanUtils.isNotEmpty(taskAppItem2)) {
            //获取分类的父路径
            GlobalType globalType = globalTypeService.getOneById(typeId);
            if (BeanUtils.isNotEmpty(globalType)) {
                String typeIdPath= globalType.getNodePath();
                String[] typeIds=typeIdPath.split("\\.");
                for (String itemTypeId : typeIds) {
                    for (TaskApprovalItems taskAppItem:taskAppItem2) {
                        if ((taskAppItem.getTypeId()).equals(itemTypeId)) {
                            taskAppItemsList.add(taskAppItem.getExpression());
                        }
                    }
                }
            }
        }
        //去除重复的元素
        this.removeDuplicate(taskAppItemsList);
        return taskAppItemsList;
    }

    /**
     * 去除重复的元素
     * @param list
     */
    private void removeDuplicate (List<?> list){
        HashSet h = new HashSet(list);
        list.clear();
        list.addAll(h);
    }

    /**
     * 根据modelKey查询
     * @param modelKey
     * @return
     */
    public List<TaskApprovalItems> queryByModelKey(String modelKey) {
        return taskApprovalItemsDao.queryByModelKey(modelKey);
    }
}
